library(tidyverse)
library(cjoint)
library(cregg)
library(xtable)

###SET WORKING DIRECTORY and LOAD FILES ----


set.seed(12345678)

white_long <- read.csv("b_data/4_white_data_for_analysis.csv")
black_long <- read.csv("b_data/5_black_data_for_analysis.csv")


###Feature order----

c_order <- c("Faces", "Identifies", "Religion", "Background", "Language",
             "Spouse", "Education", "Profession", "Political")

###Turning features into factors----

black_long <- black_long %>% 
  mutate(Faces = factor(Faces, levels = c("White man", "White woman", "Light man", "Light woman", 
                                          "Medium man",   "Medium woman", "Brown man", 
                                          "Brown woman", "Black man", "Black woman"), 
                        ordered = is.ordered(Faces)))


black_long <- black_long %>%
  mutate(Background = factor(Background, levels = c("Legal immigrant",
                                                    "Undoc. immigrant",
                                                    "Naturalized citizen",
                                                    "US-born legal parents",
                                                    "US-born undoc. parents",
                                                    "US-born legal grandparents",
                                                    "US-born undoc. grandparents")))

black_long <- black_long %>%
  mutate(Identifies = factor(Identifies, levels = c("American",
                                                    "Latino",
                                                    "Hispanic",
                                                    "Mexican-American",
                                                    "Mexican",
                                                    "White",
                                                    "Black")))

black_long <- black_long %>%
  mutate(Language = factor(Language, levels = c("Only English",
                                                "Bilingual",
                                                "Limited English",
                                                "No English"),
                           ordered = is.ordered(Language)))

black_long <- black_long %>%
  mutate(Education = factor(Education, levels = c("High school dropout",
                                                  "High school graduate",
                                                  "Bachelor's degree",
                                                  "Advanced/graduate degree"),
                            ordered = is.ordered(Education)))

black_long <- black_long %>%
  mutate(Profession = factor(Profession, levels = c("Waiter", "Janitor",
                                                    "Sales manager", "Teacher",
                                                    "IT professional", "Doctor",
                                                    "Small business owner")))

black_long <- black_long %>%
  mutate(Religion = factor(Religion, levels = c("Presbyterian", "Evangelical",
                                                "Catholic", "Not religious")))

black_long <- black_long %>%
  mutate(Political = factor(Political, levels = c("Democrat", "Republican",
                                                  "Independent", "Not political")))

black_long <- black_long %>%
  mutate(Spouse = factor(Spouse, levels = c("Not married",
                                            "S. is American",
                                            "S. is Black",
                                            "S. is White",
                                            "S. is same as profile")))


white_long <- white_long %>% 
  mutate(Faces = factor(Faces, levels = c("White man", "White woman", "Light man", "Light woman", 
                                          "Medium man",   "Medium woman", "Brown man", 
                                          "Brown woman", "Black man", "Black woman"), 
                        ordered = is.ordered(Faces)))


white_long <- white_long %>%
  mutate(Background = factor(Background, levels = c("Legal immigrant",
                                                    "Undoc. immigrant",
                                                    "Naturalized citizen",
                                                    "US-born legal parents",
                                                    "US-born undoc. parents",
                                                    "US-born legal grandparents",
                                                    "US-born undoc. grandparents")))

white_long <- white_long %>%
  mutate(Identifies = factor(Identifies, levels = c("American",
                                                    "Latino",
                                                    "Hispanic",
                                                    "Mexican-American",
                                                    "Mexican",
                                                    "White",
                                                    "Black")))

white_long <- white_long %>%
  mutate(Language = factor(Language, levels = c("Only English",
                                                "Bilingual",
                                                "Limited English",
                                                "No English"),
                           ordered = is.ordered(Language)))

white_long <- white_long %>%
  mutate(Education = factor(Education, levels = c("High school dropout",
                                                  "High school graduate",
                                                  "Bachelor's degree",
                                                  "Advanced/graduate degree"),
                            ordered = is.ordered(Education)))

white_long <- white_long %>%
  mutate(Profession = factor(Profession, levels = c("Waiter", "Janitor",
                                                    "Sales manager", "Teacher",
                                                    "IT professional", "Doctor",
                                                    "Small business owner")))

white_long <- white_long %>%
  mutate(Religion = factor(Religion, levels = c("Presbyterian", "Evangelical",
                                                "Catholic", "Not religious")))

white_long <- white_long %>%
  mutate(Political = factor(Political, levels = c("Democrat", "Republican",
                                                  "Independent", "Not political")))

white_long <- white_long %>%
  mutate(Spouse = factor(Spouse, levels = c("Not married",
                                            "S. is American",
                                            "S. is Black",
                                            "S. is White",
                                            "S. is same as profile")))

black_long$prototypical_high <- as.factor(black_long$prototypical_high)

white_long$prototypical_high <- as.factor(white_long$prototypical_high)

####Marginal mean differences by perceived prototypicality, among White respondents----
prototype_diff_w <- mm_diffs(na.omit(white_long),
                             rating_r ~ Faces + Background
                             + Education + Profession
                             + Language + Political
                             + Religion + Spouse
                             + Identifies,
                             id = ~ caseid,
                             by = ~ prototypical_high,
                             weights = ~weight,
                             feature_order = c_order)

prototype_diff_w_p <- plot(prototype_diff_w)

###Figure A5 ----
figure_A5 <- prototype_diff_w_p + ggplot2::theme_bw() + 
  theme(legend.position="none", plot.title= element_text(size=10, hjust=0.5)) +
  scale_color_manual(values = rep("black", 9)) +
  geom_point(size = 2) + 
  ggplot2::labs(x = "Estimated Difference Between High and Low Prototypicality Respondents",
                title = "Marginal Mean Differences in Perceived Americanness \n by Prototypicality Perceptions Among White Respondents")

ggsave("c_figures/figure_A5.pdf", width=8.5, height=11, plot = figure_A5)

###Table A13----
prototype_diff_w_table <- prototype_diff_w[c(5:9)]
print.xtable(xtable(prototype_diff_w_table), "d_tables/table_A13.tex", type = "latex", include.rownames=FALSE)


####Marginal mean differences by perceived prototypicality, among Black respondents----
prototype_diff_b <- mm_diffs(na.omit(black_long),
                              rating_r ~ Faces + Background
                              + Education + Profession
                              + Language + Political
                              + Religion + Spouse
                              + Identifies,
                              id = ~ caseid,
                              by = ~ prototypical_high,
                              weights = ~weight,
                              feature_order = c_order)




###Figure A6----
prototype_diff_b_p <- plot(prototype_diff_b)
figure_A6 <- prototype_diff_b_p + ggplot2::theme_bw() + 
  theme(legend.position="none", plot.title= element_text(size=10, hjust=0.5)) +
  scale_color_manual(values = rep("black", 9)) +
  geom_point(size = 2) + 
  ggplot2::labs(x = "Estimated Difference Between High and Low Prototypicality Respondents",
                title = "Marginal Mean Differences in Perceived Americanness \n by Prototypicality Perceptions Among Black Respondents")

ggsave("c_figures/figure_A6.pdf", width=8.5, height=11, plot = figure_A6)

###Table A13----
prototype_diff_b_table <- prototype_diff_b[c(5:9)]
print.xtable(xtable(prototype_diff_b_table), "d_tables/table_A14.tex", type = "latex", include.rownames=FALSE)

#### Clear environment ----

rm(list = ls())
